<script setup lang="ts">
import { nextTick, onMounted, reactive, ref } from 'vue'
import {
  addOrUpdateMenu,
  deleteMenu,
  getMenuData,
} from '@/api/acl/permission/index.ts'
import { ElMessage } from 'element-plus'
const menuData = ref()
const menuParams = reactive<any>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})
const dialogFormVisible = ref(false)
onMounted(() => {
  getMenuDatas()
})
const getMenuDatas = async () => {
  let res = await getMenuData()
  if (res.code == 200) {
    menuData.value = res.data
  }
}
const addMenu = (row: any) => {
  Object.assign(menuParams, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  dialogFormVisible.value = true
  menuParams.pid = row.id
  menuParams.level = row.level + 1
  nextTick(() => {
    ruleFormRef.value.clearValidate('name')
    ruleFormRef.value.clearValidate('code')
  })
}
const editMenu = (row: any) => {
  Object.assign(menuParams, row)
  dialogFormVisible.value = true
  nextTick(() => {
    ruleFormRef.value.clearValidate('name')
    ruleFormRef.value.clearValidate('code')
  })
}
const deleteMenuById = async (id: number) => {
  let res = await deleteMenu(id)
  if (res.code == 200) {
    await getMenuDatas()
    ElMessage({ type: 'success', message: '删除成功' })
  } else {
    ElMessage({ type: 'success', message: '删除失败' })
  }
}
const ruleFormRef = ref()
const submitForm = async () => {
  await ruleFormRef.value.validate()
  let res = await addOrUpdateMenu(menuParams)
  if (res.code == 200) {
    dialogFormVisible.value = false
    ElMessage({
      type: 'success',
      message: menuParams.id ? '修改成功' : '添加成功',
    })
    await getMenuDatas()
  }
}
const validatorName = (_: any, value: any, callBack: any) => {
  if (value.trim().length > 0) {
    callBack()
  } else {
    callBack(new Error('名称不能为空'))
  }
}
const validatorCode = (_: any, value: any, callBack: any) => {
  if (value.trim().length > 0) {
    callBack()
  } else {
    callBack(new Error('权限值不能为空'))
  }
}
const rules = {
  name: [{ required: true, trigger: 'blur', validator: validatorName }],
  code: [{ required: true, trigger: 'blur', validator: validatorCode }],
}
</script>

<template>
  <el-card>
    <el-table
      :data="menuData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="code" label="权限值" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column label="操作" width="350px" align="center">
        <template #="{ row }">
          <el-button
            type="primary"
            plain
            icon="User"
            @click="addMenu(row)"
            :disabled="row.level == 4"
            v-has="`btn.Permission.add`"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="warning"
            plain
            icon="Edit"
            @click="editMenu(row)"
            :disabled="row.level == 1"
            v-has="`btn.Permission.update`"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`你确定删除'${row.name}'吗？`"
            @confirm="deleteMenuById(row.id)"
            width="250"
          >
            <template #reference>
              <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="row.level == 1"
                v-has="`btn.Permission.remove`"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="dialogFormVisible"
      :title="menuParams.id ? '编辑菜单' : '添加菜单'"
    >
      <el-form
        :model="menuParams"
        ref="ruleFormRef"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="menuParams.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="权限值" prop="code">
          <el-input v-model="menuParams.code" placeholder="请输入权限值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<style scoped lang="scss"></style>
